﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormUpdateOrder : FormTemplet
    {
        private int _nCustomerID = -1;

        private int _nProjectID = -1;

        private string _sOrderCode = null;

        public FormUpdateOrder()
        {
            this.InitializeComponent();
        }

        public FormUpdateOrder(string sOrderCode) : this()
        {
            this._sOrderCode = sOrderCode;
            DataTable dataTable = null;
            Orders orders = new Orders();
            try
            {
                dataTable = orders.LoadOrderForUpdate(this._sOrderCode);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            orders.Dispose();
            if (dataTable != null)
            {
                if (dataTable.Rows.Count > 0)
                {
                    this.txtOrderCode.Text = dataTable.Rows[0][1].ToString();
                    this._nCustomerID = Convert.ToInt32(dataTable.Rows[0][2]);
                    this.txtCustomerName.Text = dataTable.Rows[0][3].ToString();
                    this._nProjectID = Convert.ToInt32(dataTable.Rows[0][4]);
                    this.txtProjectName.Text = dataTable.Rows[0][5].ToString();
                    this.dtpSpeakForDate.Value = DateTime.Parse(dataTable.Rows[0][7].ToString());
                    this.txtRemark.Text = dataTable.Rows[0][8].ToString();
                    this.txtOperator.Text = dataTable.Rows[0][9].ToString();
                    this.btnOk.Enabled = true;
                }
                else
                {
                    MessageBox.Show("数据库里没有查到此订单的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                dataTable.Dispose();
            }
        }

        private void btnSelectCustomer_Click(object sender, EventArgs e)
        {
            CustomerData customerData = FormSelectCustomer.ShowBox();
            if (customerData != null)
            {
                this._nCustomerID = customerData.CustomerID;
                this.txtCustomerName.Text = customerData.CompanyName + "(" + customerData.Linkman + ")";
                this._nProjectID = -1;
                this.txtProjectName.Text = "";
                this.btnSelectProject.Focus();
            }
        }

        private void btnSelectProject_Click(object sender, EventArgs e)
        {
            if (this.txtCustomerName.Text.Trim() == "" || this._nCustomerID == -1)
            {
                MessageBox.Show("请选择客户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.btnSelectCustomer.Focus();
            }
            else
            {
                ProjectData projectData = FormSelectProject.ShowBox(this._nCustomerID);
                if (projectData != null)
                {
                    this._nProjectID = projectData.ProjectID;
                    this.txtProjectName.Text = projectData.ProjectName + "(" + projectData.Linkman + ")";
                    this.txtRemark.Focus();
                }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this._sOrderCode == null || this.txtOrderCode.Text.Trim().Length < 1)
            {
                MessageBox.Show("没有找到此订单！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this._nCustomerID == -1 || this.txtCustomerName.Text.Trim().Length < 1)
            {
                MessageBox.Show("请选择客户", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.btnSelectCustomer.Focus();
            }
            else if (this._nProjectID == -1 || this.txtProjectName.Text.Trim().Length < 1)
            {
                MessageBox.Show("请选择对此客户的计划项目", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.btnSelectProject.Focus();
            }
            else if (this.txtRemark.Text.Trim().Length > 64)
            {
                MessageBox.Show("备注字符太长", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtRemark.Focus();
            }
            else
            {
                Orders orders = new Orders();
                bool flag = false;
                try
                {
                    flag = orders.UpdateOrder(this._sOrderCode, this._nProjectID, this.txtRemark.Text.Trim());
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                orders.Dispose();
                if (flag)
                {
                    MessageBox.Show("订单修改成功", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    base.Close();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void FormUpdateOrder_Activated(object sender, EventArgs e)
        {
            base.WindowState = FormWindowState.Normal;
        }
    }
}
